﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jemer.Domain;
using Jemer.Data;
using Jemer.Data.Infrastructure;
using Jemer.Domain.Entities;
using Jemer.Data.Repositories;

namespace Jemer.Service
{
    
    public interface IUserInfoService
    {
        IQueryable<UserInfo> GetUserInfos();
        UserInfo GetUserInfo(int id);
        void CreateUserInfo(UserInfo userInfo);
        void DeleteUserInfo(int id);
        void SaveUserInfo();
    }
    
    public class UserInfoService : IUserInfoService
    {
        private readonly IUserInfoRepository userInfoRepository;
        private readonly IUnitOfWork unitOfWork;
        public UserInfoService(IUserInfoRepository userInfoRepository, IUnitOfWork unitOfWork)
        {
            this.userInfoRepository = userInfoRepository;
            this.unitOfWork = unitOfWork;
        }  
        #region IUserInfoService Members

        public IQueryable<UserInfo> GetUserInfos()
        {
            var userInfos = userInfoRepository.GetAll();
            return userInfos;
        }

        public UserInfo GetUserInfo(int id)
        {
            var userInfo = userInfoRepository.GetById(id);
            return userInfo;
        }

        public void CreateUserInfo(UserInfo userInfo)
        {
            userInfoRepository.Add(userInfo);
            SaveUserInfo();
        }

        public void DeleteUserInfo(int id)
        {
            var userInfo = userInfoRepository.GetById(id);
            userInfoRepository.Delete(userInfo);
            SaveUserInfo();
        }

        public void SaveUserInfo()
        {
            unitOfWork.Commit();
        }

        #endregion
    }
    
    
}